import unittest
import client as c

URL = "192.168.2.108:4000"

class TestStringMethods(unittest.TestCase):    
    def test_get_blocknumber(self):
        bn = c.brochain_get_blocknumber(URL)
        # print("bnnnnnnnnnnnnnnnnnnnnnnnn: {}".format(bn))
        self.assertTrue(bn > 0)

    def test_brochain_call(self):
        region_id = "0x000000000000005b00000000654c4d6800000000"
        from_addr = "0x802513a783a01c371d0b057aa3e1fcfe0b7af597"
        to_addr = "0x000000000000005b00000000654c4d6800000000"
        abi = {
            "inputs": [
                {
                "internalType": "string",
                "name": "did",
                "type": "string"
                }
            ],
            "name": "getDidDocument",
            "outputs": [
                {
                "internalType": "string",
                "name": "didDocument",
                "type": "string"
                }
            ],
            "stateMutability": "view",
            "type": "function",
        }
        params = ["did:tnps:V5hThwWJpcdqe5cGC1yFBy"]

        res = c.brochain_call(URL,
                            region_id,
                            from_addr,
                            to_addr,
                            abi,
                            params)
        res_expected = '{"created":"2023-11-09T11:13:15Z","service":[{"id":"did:tnps:V5hThwWJpcdqe5cGC1yFBy#did-communication","serviceEndpoint":"http://192.168.8.67:8080","type":"did-communication"}],"id":"did:tnps:V5hThwWJpcdqe5cGC1yFBy","publicKey":[{"controller":"did:tnps:V5hThwWJpcdqe5cGC1yFBy","publicKeyHex":"e367cacd50feeb049154fa777bf324543e042042ccbff1849f1ada795ad1decb1254bd4f3a68345f526e9e1ca875807876b49b37a04ff0688ced432087795b8b","id":"did:tnps:V5hThwWJpcdqe5cGC1yFBy#key-1","type":"SM2"}],"updated":"2023-11-09T11:13:15Z","@context":"https://w3id.org/did/v1","authentication":[{"controller":"did:tnps:V5hThwWJpcdqe5cGC1yFBy","publicKeyHex":"e367cacd50feeb049154fa777bf324543e042042ccbff1849f1ada795ad1decb1254bd4f3a68345f526e9e1ca875807876b49b37a04ff0688ced432087795b8b","id":"did:tnps:V5hThwWJpcdqe5cGC1yFBy#key-1","type":"SM2"}]}'
        self.assertEqual(res, res_expected)

    def test_brochain_send_tx(self):
        region_id = "0x000000000004966900000000656820bc00000000"
        from_addr = "0x87b86349bbb92740548375fa152bb7f793c8dfd4"
        to_addr = "0xa61a6fb862c6b4d930790f65925709916b13abf7"
        private_key = "28e6fa30d7285824ab67e258f098a1fb13b2f49eba8f6c832344c436edc6aea5"
        abi = {
            "inputs": [
                {"internalType": "string","name": "key","type": "string"},
                {"internalType": "bytes","name": "value","type": "bytes"}
            ],
            "name": "store","outputs": [],
            "stateMutability": "nonpayable",
            "type": "function",
        }

        result = c.brochain_send_tx(URL, region_id, to_addr, abi, ['hello', b'world'], private_key)

        print("result:\n{}".format(result))

if __name__ == '__main__':
    unittest.main()